package com.david.concurrent.notify.wait;

/**
 * @author wade
 * @version 1.0
 * @date 2021/7/19 17:21
 */
public class NotifyDemo {
    private static final Object resource=new Object();

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            synchronized (resource){
                System.out.println("t1 get resource lock!");
                try {
                    System.out.println("t1 begin wait!");
                    resource.wait();
                    System.out.println("t1 end wait!");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        Thread t2 = new Thread(() -> {
            synchronized (resource){
                System.out.println("t2 get resource lock!");
                try {
                    System.out.println("t2 begin wait!");
                    resource.wait();
                    System.out.println("t2 end wait!");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        Thread t3 = new Thread(() -> {
            synchronized (resource){
                System.out.println("t3 begin notify");
                resource.notify();
            }
        });
        t1.start();
        t2.start();
        Thread.sleep(1000);
        t3.start();
        t1.join();
        t2.join();
        t3.join();
    }
}
